﻿namespace IBTrader.Charts.Drawing
{
    using IBTrader.Indicator.CandleStick;
    using IBTrader.Modules.Read;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;

    class ChartCandleStick : BaseChart
    {
        public ChartCandleStick(Worker worker) : base(worker) { }
        public override void Draw(IGraphics g, Charts.Region region)
        {
            var size = ItemWidth(region);
            foreach (Item item in Lines)
            {
                var x = region.x(item.OpenLine) + size;
                var width = region.x(item.CloseLine) - region.x(item.OpenLine);
                var height = Math.Max(1f, region.y(region.Y.Max - item.BodyHeight));
                var r = new RectangleF(x - size, region.y(item.BodyTop), width, height);
                g.DrawLines(Pen(), new PointF(x, region.y(item.High)), new PointF(x, region.y(item.Low)));
                g.FillRectangle(Brush(item.Up ? Color.Green : Color.Red), r);
                g.DrawRectangle(Pen(), Rectangle.Round(r));
            }
        }
        private float ItemWidth(Charts.Region region)
        {
            if (!Lines.Any()) return 0;
            //var firstBar = Lines.First() as Item;
            //var totalSize = Worker.Configuration.To - Worker.Configuration.From;
            //var barSize = firstBar.CloseLine.DateTime - firstBar.OpenLine.DateTime;
            //var barCount = barSize.Ticks == 0 ? 0 : totalSize.Ticks / barSize.Ticks;
            //var width = Math.Max(1f, region.Y.Total / barCount);
            var width = Math.Max(1f, region.Y.Total / Lines.Count);
            return width;
        }
    }
}